<?php
class Controller_Admin_Resort extends Controller_Admin_Base {
		
	protected function actionSwitch() {
		switch ($this->arrRequest[2]) {
			case '': $this->actionResortList(); break;
 			case 'add': $this->actionAddResort(); break;
			case 'edit':
				$resortId = intval($this->arrRequest[3]);
				$objResort = new Resort($resortId);
				if (!$objResort->id) throw new Exception_SiteError('', 404);
				$this->actionEditResort($objResort);
				break;
			case 'gallery':
				$resortId = intval($this->arrRequest[3]);
				$objResort = new Resort($resortId);
				if (!$objResort->id) throw new Exception_SiteError('', 404);
				$this->actionGallery($objResort);
				break;
			default: throw new Exception_SiteError('', 404);
		}
	}
	
	/**
	 * Список всех курортов
	 */
	private function actionResortList() {
		$objResort = new Resort();
		
		$arrResorts = $objResort->find('all');
		
		$this->pageHead = 'Список курортов (<a href="/admin/resort/add">Добавить</a>)';
		$this->title = 'Список курортов';
		
		$this->showHeader();
			View_Resort::showAdminResortList($arrResorts);
		$this->showFooter();
	}
	
	/**
	 * Страница добавления курорта
	 */
	private function actionAddResort() {
		if (isset($_POST['Resort'])) {
			$objResort = new Resort($_POST['Resort']);
			if ($objResort->save()) User_Location::move('/admin/resort');
			else print_r($objResort->arrErrors);
		}
		else $objResort = new Resort();
		
		$objCountry = new Country();
		$arrCountries = $objCountry->find('all', array(
			'select' => '`ID_country`, `country_name`',
			'order' => '`country_name` ASC'
		));
		
		$this->pageHead = $this->title = 'Добавление курорта';
		
		$this->showHeader();
			View_Resort::showResortForm($objResort, $arrCountries);
		$this->showFooter();
	}
	
	/**
	 * Страница редактирования курорта
	 * @param Resort $objResort - объект курорта
	 */
	private function actionEditResort(Resort $objResort) {
		if (isset($_POST['Resort'])) {
			$objResort = new Resort($_POST['Resort']);
			if ($objResort->save()) User_Location::move('/admin/resort');
			else print_r($objResort->arrErrors);
		}
		
		$objCountry = new Country();
		$arrCountries = $objCountry->find('all', array(
				'select' => '`ID_country`, `country_name`',
				'order' => '`country_name` ASC'
		));
		
		$this->pageHead = $this->title = 'Редактирование страны';
		
		$this->showHeader();
			View_Resort::showResortForm($objResort, $arrCountries);
		$this->showFooter();
	}
	
	/**
	 * Галерея курорта
	 * @param Resort $objResort
	 */
	private function actionGallery(Resort $objResort) {
		if (!empty($_FILES)) {
			Photo::savePhotosFromFiles(Resort::getEssenceType(), $objResort->id);
			$objResort = new Resort($objResort->id);
		}
	
		$arrPhotos = $objResort->getPhotosIds();
		$this->title = $this->pageHead = $objResort->getField('resort_name') . ' - галерея';
	
		$this->showHeader();
			View_Photo::showPhotos($arrPhotos, $objResort->getField('ID_photo'));
			View_Photo::showGalleryForm($objResort);
		$this->showFooter();
	}
	
}